const app = getApp();
Page({
  data: {
    TabCur: 0,
    scrollLeft: 0,
    list: null,
  },
  onShow() {
    this.init();
  },
  goInfo(e) {
    var gramIds = e.currentTarget.dataset.gramIds;
    console.log('gramIds from data attribute:', gramIds);
    if (!gramIds) {
      console.error('gramIds is undefined or empty');
      return;
    }
    var gramIdsArray = gramIds.split(',');
    console.log('gramIdsArray:', gramIdsArray);
    var gameId = gramIdsArray[0];
    console.log('gameId:', gameId);
    wx.navigateTo({
      url: '/pages/activity/info/info?id=' + gameId
    });
  },
  init() {
    var that = this;  
    var userId = app.globalData.userId;
    wx.request({
      url: app.globalData.local_url + 'api/myOrder',
      data: {
        userId: userId
      },
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log('Response:', res);
        if (res.data.code == 200) {
          that.setData({
            list: res.data.data
          });   
        } else {
          console.error('Error response:', res);
        }
      },
      fail: function (error) {
        console.error('Request failed:', error);
      }
    });
  },
  delete(e) {
    var that = this;  
    var id = e.currentTarget.dataset.id;
    wx.request({
      url: app.globalData.local_url + 'api/deleteOrder',
      data: {
        id: id
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log('Delete response:', res);
        if (res.data.code == 200) {
          wx.showToast({
            title: '订单删除成功',
            icon: 'success',
            duration: 2000 // 持续的时间
          });
          that.init();
        } else {
          console.error('Delete error response:', res);
        }
      },
      fail: function (error) {
        console.error('Delete request failed:', error);
      }
    });
  }
});